package com.softer.chengxin.district.controller;

import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.district.entity.District;
import com.softer.chengxin.district.entity.DistrictStreet;
import com.softer.chengxin.district.service.DistrictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zcz
 * @date 2018/02/20
 */
@RestController
@RequestMapping("/api/district")
@Slf4j
public class DistrictController {
    @Autowired
    private DistrictService districtService;

    /**
     * 查询地区树
     * @return
     */

    @RequestMapping("/getDistrictStreet")
    List<DistrictStreet> getDistrictStreet(){
        return districtService.getDistrictStreet();
    }
    /**
     * 查询列表,填充下拉框
     * @param id
     * @return
     */
    @RequestMapping("getDistrictByParentId")
    List<District> getDistrictByParentId(String id){
        return districtService.getDistrictByParentId(id);
    }

    /**
     * 新增
     * @param district
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(District district, HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        return districtService.add(district);
    }
    /**
     * 根具id查詢地區信息，填充修改框
     * @param id
     * @return
     */
    @RequestMapping("/getDistrictById")
    public District getDistrictById(String id){
        return districtService.getDistrictById(id);
    }

    /**
     * 修改
     * @param district
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(District district,HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        return districtService.update(district);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(String id,HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        return districtService.delete(id);
    }

}
